package Offer2018;

/**
 * 旋转数组的最小值
 * Created by 雷文康 on 2018/3/3.
 */
public class Test08 {

    public static void main(String[] args) {
        int[] array = {1,1,0,1,1};
        System.out.println(findMin(array));
    }

    public static int findMin(int[] array) {
        if (array == null || array.length == 0) {
            System.out.println();
            return -1;
        }

        if (array[0] < array[array.length - 1]) {
            return array[0];
        }
        if (array[0] == array[array.length - 1] && array[0] == array[(array.length -1)/2]) {
             return -1;
        }

        int start = 0;
        int end = array.length - 1;
        while (end - start > 1) {
            int mid = (start + end) / 2;
            if (array[mid] <= array[end]) {
                end = mid;
            } else if (array[mid] >= array[start]) {
                start = mid;
            }
        }
        return array[end];
    }
}
